import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/views/Layout.vue'),
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'lost',
        name: 'Lost',
        component: () => import('@/views/Lost.vue'),
        meta: { title: '失物查询' }
      },
      {
        path: 'found',
        name: 'Found',
        component: () => import('@/views/Found.vue'),
        meta: { title: '招领查询' }
      },
      {
        path: 'publish',
        name: 'Publish',
        component: () => import('@/views/Publish.vue'),
        meta: { title: '发布信息', requiresAuth: true }
      },
      {
        path: 'my-publish',
        name: 'MyPublish',
        component: () => import('@/views/MyPublish.vue'),
        meta: { title: '我的发布', requiresAuth: true }
      },
      {
        path: 'my-claims',
        name: 'MyClaims',
        component: () => import('@/views/MyClaims.vue'),
        meta: { title: '我的认领', requiresAuth: true }
      },
      {
        path: 'announcement',
        name: 'Announcement',
        component: () => import('@/views/Announcement.vue'),
        meta: { title: '公告' }
      },
      {
        path: 'admin',
        name: 'Admin',
        component: () => import('@/views/Admin.vue'),
        meta: { title: '后台管理', requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'detail/:id',
        name: 'Detail',
        component: () => import('@/views/Detail.vue'),
        meta: { title: '详情' }
      },
      // 超级管理员路由
      {
        path: 'super-admin',
        name: 'SuperAdmin',
        component: () => import('@/views/SuperAdmin.vue'),
        meta: { title: '超级管理员', requiresAuth: true, requiresSuperAdmin: true }
      },
      {
        path: 'user-manage',
        name: 'UserManage',
        component: () => import('@/views/UserManage.vue'),
        meta: { title: '用户管理', requiresAuth: true, requiresSuperAdmin: true }
      },
      {
        path: 'role-manage',
        name: 'RoleManage',
        component: () => import('@/views/RoleManage.vue'),
        meta: { title: '角色管理', requiresAuth: true, requiresSuperAdmin: true }
      },
      {
        path: 'permission-manage',
        name: 'PermissionManage',
        component: () => import('@/views/PermissionManage.vue'),
        meta: { title: '权限管理', requiresAuth: true, requiresSuperAdmin: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const isLoggedIn = userStore.isLoggedIn
  const roles = userStore.userInfo.roles || []

  if (to.meta.requiresAuth && !isLoggedIn) {
    next('/login')
  } else if (to.meta.requiresAdmin && !roles.includes('ADMIN') && !roles.includes('SUPER_ADMIN')) {
    // 需要管理员权限但用户没有 ADMIN 或 SUPER_ADMIN 角色
    next('/')
  } else if (to.meta.requiresSuperAdmin && !roles.includes('SUPER_ADMIN')) {
    // 需要超级管理员权限但用户没有 SUPER_ADMIN 角色
    next('/')
  } else {
    next()
  }
})

export default router

